//
//  YyYViewController.m
//  何五路
//
//  Created by _ADY on 15/8/5.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "YyYViewController.h"

@interface YyYViewController ()

@end

@implementation YyYViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.alpha = 0.0;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBar.alpha = 1;
    [super viewWillDisappear:animated];
}

-(void)popself
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = bgColor;
    
    UIImageView *bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yyyBg"]];
    [bgImage setFrame:self.view.frame];
    [self.view addSubview:bgImage];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;
    
    UIButton *bar1Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    bar1Button.frame = CGRectMake(4, 27,27, 28);
    [bar1Button addTarget:self action:@selector(popself) forControlEvents:UIControlEventTouchUpInside];
    [bar1Button setBackgroundImage:[UIImage imageNamed:@"fhui"] forState:UIControlStateNormal];
    [self.view addSubview:bar1Button];
    
    
    
    NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"1@2x" ofType:@"gif"]];
    // view生成 CGRectMake(0,0,640/2,1136*self.view.frame.size.width/(640*1))
    webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
    webView.center =CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2+150);
    webView.userInteractionEnabled = NO;//用户不可交互
    webView.backgroundColor = [UIColor clearColor];
    webView.opaque = NO;
    webView.scalesPageToFit = YES;
    [webView loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
    [self.view addSubview:webView];

    // 设置允许摇一摇功能
    [UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;
    // 并让自己成为第一响应者
    [self becomeFirstResponder];
    
//    LotteryInfo
    /*
     /ApiLottery/LotteryInfo
     
     {"Entity":{
     "CustomerID":796,//用户ID
     "ActivityIntegral":0,//可用银元宝
     "LotteryPoints":20,//每次抽奖要花费的银元宝数量
     "LotteryFreeTimes":2,//每天可免费抽奖次数
     "LotteryTotalTimes":10,//每天可抽奖次数,为0则为不限制
     "UsedTimes":0,//已抽奖次数
     "WinSetting":[{//奖励设置
     "Title":"二等奖","Rate":0.01,"Points":300,"Id":2},{//points为奖励元宝数
     "Title":"三等奖","Rate":0.05,"Points":100,"Id":3},{
     "Title":"一等奖","Rate":0.005,"Points":500,"Id":7},{
     "Title":"参与奖","Rate":0.99,"Points":20,"Id":8}]},
     
     "PageEntity":null,"Result":"success","Message":null}
     */
    
}

-(void)GOto
{
    aYbInt = 0;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        NSDictionary *ret = [SendService Lottery:nil];
        dispatch_async(dispatch_get_main_queue(), ^{
            
//            NSLog(@"ret %@",ret);
            if (![[NSString stringWithFormat:@"%@",[ret objectForKey:@"Result"]] isEqualToString:@"error"])
            {
                NSString *aYbString = [[ret objectForKey:@"Entity"] objectForKey:@"WinPoints"];
                aYbInt = [aYbString intValue];
            }
            NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"3@2x" ofType:@"gif"]];
            [webView loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
            
            sound = kSystemSoundID_Vibrate;//震动
            AudioServicesPlaySystemSound(sound);
            
            [self gotoView];

            
        });
    });
    
}
-(void)gotoView
{
    if (bgView != nil) {
        [bgView removeFromSuperview];
        bgView = nil;
    }
    bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 447/2, 268/2)];
    bgView.center =CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
    [self.view addSubview:bgView];
    
    UIImageView *bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"YyYBg1"]];
    bgImage.frame =CGRectMake(0, 0, 447/2, 268/2);
    [bgView addSubview:bgImage];
    
    UIButton *sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sureButton.frame = CGRectMake((447/2-137/2)/2, 268/2-51/2-15, 137/2, 51/2);
    [sureButton setImage:[UIImage imageNamed:@"YyYSure"] forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(gotAction) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:sureButton];
    
    if (aYbInt <= 0)
    {
        UIImageView *bg1Image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"YyYNot"]];
        bg1Image.frame =CGRectMake((447/2-365/2)/2, 20, 365/2, 100/2);
        [bgView addSubview:bg1Image];
    }
    else
    {
        UIImageView *bg1Image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"YyYYes"]];
        bg1Image.frame =CGRectMake((447/2-345/2)/2, 20, 345/2, 37/2);
        [bgView addSubview:bg1Image];
        
        NSDictionary *attribute = @{NSFontAttributeName: [UIFont fontWithName:@"TrebuchetMS-Bold" size:labelSize+6]};
        CGSize totalTextSize = [[NSString stringWithFormat:@"%d",aYbInt] boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 45) options: NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
        
        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake((447/2-345/2)/2+40, 20+37/2+10, totalTextSize.width, 45)];
        label1.text = [NSString stringWithFormat:@"%d",aYbInt];
        [label1 setFont:[UIFont fontWithName:@"TrebuchetMS-Bold" size:labelSize+6]];
        label1.backgroundColor = [UIColor clearColor];
        label1.textColor = [UIColor redColor];
        label1.textAlignment = NSTextAlignmentRight;
        [bgView addSubview:label1];
        
        UIImageView *bg2Image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"YyYGe"]];
        bg2Image.frame =CGRectMake((447/2-345/2)/2+totalTextSize.width+40, 20+37/2+20,41/2, 41/2);
        [bgView addSubview:bg2Image];
        
        
    }
}

-(void)gotAction
{
    if (bgView != nil) {
        [bgView removeFromSuperview];
        bgView = nil;
    }
    NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"1@2x" ofType:@"gif"]];
    [webView loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
}

#pragma mark - 摇一摇相关方法
// 摇一摇开始摇动
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
//    NSLog(@"开始摇动");
    NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"2@2x" ofType:@"gif"]];
    [webView loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
    
    [self GOto];
    
    return;
}

// 摇一摇取消摇动
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
//    NSLog(@"取消摇动");
    return;
}

// 摇一摇摇动结束
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (event.subtype == UIEventSubtypeMotionShake) { // 判断是否是摇动结束
//        NSLog(@"摇动结束");

    }
    return;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
